#include "sum_op.h"
#include "../../matrix/matrix.h"
#include "sum_grad_op.h"
#include "../../factory/sum_grad_factory.h"

Sum::Sum(Node* node) : OpNode({node}){
    setOutShape(new Shape(1, 1));
}

vector<Node*> Sum::getGradNodes(Node* grad){
    auto nodes = *getNodeIn();
    Shape* shape = nodes[0] -> getOutShape();
    Factory* factory = new SumGradFactory(grad, shape);
    Node* ret_grad = factory -> createNode();
    delete factory;
    return { ret_grad };
}

void Sum::compute(Context* context){
    auto nodes = *getNodeIn();
    Matrix mat = context -> getFlowedData(nodes[0]);
    context -> setFlowedData(this,
            Matrix(1, 1, sum(mat)));
}
